package com.newsystem.service.impl;

import com.newsystem.MyUtils.CommonResult;
import com.newsystem.MyUtils.MyUUIDUtils;
import com.newsystem.mapper.AdminMapper;
import com.newsystem.mapper.GeneralUserMapper;
import com.newsystem.mapper.MediumMapper;
import com.newsystem.pojo.Admin;
import com.newsystem.pojo.GeneralUser;
import com.newsystem.pojo.Medium;
import com.newsystem.pojo.User;
import com.newsystem.mapper.UserMapper;
import com.newsystem.pojo.vo.AdminInfo;
import com.newsystem.pojo.vo.GeneralUserInfo;
import com.newsystem.pojo.vo.MediumInfo;
import com.newsystem.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 使用者 服务实现类
 * </p>
 *
 * @author chenlitao
 * @since 2022-11-06
 */
@Service
public class UserService extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private GeneralUserMapper generalUserMapper;
    @Autowired
    private MediumMapper mediumMapper;

    public CommonResult<Object> insterUser(String account, String password, int role, String phone) {
        List<User> userList = userMapper.selectByAccount(account);
        if(userList.size()>0){
            return CommonResult.failed("已经有此账号名");
        }
        User user = new User();
        Admin admin = new Admin();
        Medium medium = new Medium();
        GeneralUser generalUser = new GeneralUser();
        user.setId(MyUUIDUtils.getUUID());
        user.setStatue(1);
        user.setAvatar("http://162.14.78.140:9000/test/login.jpg");
        user.setAccount(account);
        user.setPassword(password);
        user.setRole(role);
        user.setPhone(phone);
        switch (role){
            case 0://管理员
            {
                admin.setId(MyUUIDUtils.getUUID());
                admin.setUserId(user.getId());
                admin.setSex(0);
                admin.setRealname("小陈");
                admin.setAddress("桂电");
                break;
            }
            case 1://普通用户
                generalUser.setId(MyUUIDUtils.getUUID());
                generalUser.setUserId(user.getId());
                generalUser.setSex(0);
                generalUser.setCollectionNum(0);
                generalUser.setFollowNum(0);
                generalUser.setCommentNum(0);
                generalUser.setIntroduce("自我介绍");
                generalUser.setQqNum("123");
                generalUser.setRealName("小立");
                break;
            case 2://媒体号
                medium.setId(MyUUIDUtils.getUUID());
                medium.setUserId(user.getId());
                medium.setFansNum(0);
                medium.setWorkNum(0);
                medium.setCompany("创元集团");
                medium.setIntroduce("自我介绍");
                medium.setMediumName("创元媒体");
                medium.setMicroBlog("chen");
                break;
        }
        if(userMapper.insert(user) > 0){
            switch (role){
                case 0:
                    adminMapper.insert(admin);
                    break;
                case 1:
                    generalUserMapper.insert(generalUser);
                    break;
                case 2:
                    mediumMapper.insert(medium);
                    break;
            }
            return CommonResult.success("注册成功");
        }
        return CommonResult.failed("注册失败");
    }

    public CommonResult<Object> selectUserInfo(String userId) {
        User user = userMapper.selectById(userId);
        AdminInfo adminInfo=null;
        GeneralUserInfo generalInfo=null;
        MediumInfo mediumInfo=null;
        if(user == null || user.getStatue() == 2){
            return CommonResult.failed("查无此用户");
        }
        switch (user.getRole()){
            case 0:
                adminInfo = userMapper.selectAdmin(userId);
                break;
            case 1:
                generalInfo = userMapper.selectGeneralUser(userId);
                break;
            case 2:
                mediumInfo = userMapper.selectMedium(userId);
                break;
        }
        if(adminMapper==null && mediumInfo==null && mediumInfo==null){
            return CommonResult.failed("获取消息失败");
        }
        Map map = new HashMap();
        map.put("adminInfo",adminInfo);
        map.put("generalInfo",generalInfo);
        map.put("mediumInfo",mediumInfo);
        return CommonResult.success(map);
    }
}
